﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class updateTeacherInfo : Form
    {
        public updateTeacherInfo()
        {
            InitializeComponent();
        }

        private void txt_tname_Click(object sender, EventArgs e)
        {
            string tno = txt_tno.Text;
            string sql = "select count(*) from dbo.teacher where tno=@tno";
            SqlParameter[] arraysql = { new SqlParameter("@tno", tno) };
            object o = DBAccess.GetOne(sql, arraysql);
            if ((int)o == 1)
            {
                txt_tishi.Text = "✓";
                txt_tishi.ForeColor = Color.Green;
            }
            else
            {
                MessageBox.Show("请输入正确的教师号");
                txt_tno.Focus();
                return;
            }
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string tno = txt_tno.Text;
            string tname = txt_tname.Text;
            string tgender = txt_tgender.Text;
            string tage = txt_tage.Text;
            if (string.IsNullOrEmpty(tno) || string.IsNullOrEmpty(tname) || string.IsNullOrEmpty(tgender) || string.IsNullOrEmpty(tage))
            {
                MessageBox.Show("信息输入不完整");
                return;
            }
            int flag = -1;
            if (int.TryParse(tgender, out flag))
            {
                if (flag == 0 || flag == 1)
                {

                }
                else
                {
                    MessageBox.Show("请输入0或1代表性别！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0或1代表性别！");
                return;
            }
            if (int.TryParse(tage, out flag))
            {
                if (flag < 0 || flag > 150)
                {
                    MessageBox.Show("请输入0-150之间的整型数值！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0-150之间的整型数值！");
                return;
            }
            string sql = "update dbo.teacher set tname=@tname,tgender=@tgender,tage=@tage where tno=@tno";
            SqlParameter[] arraysql = { new SqlParameter("@tname", tname), new SqlParameter("@tgender", tgender),
                                      new SqlParameter("@tage",tage),new SqlParameter("@tno",tno)};
            int res = DBAccess.GetState(sql, arraysql);
            if (res != 0)
            {
                MessageBox.Show("修改该教师信息成功");
                this.Dispose();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
